package com.spark.colliery.module.home.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.view.View
import android.view.animation.Animation
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.labo.kaji.fragmentanimations.CubeAnimation
import com.spark.colliery.R
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.alert
import com.spark.rectify.RectInputActivity
import com.spark.rectify.RectHistoryFragment
import com.spark.rectify.RectifyManageFragment
import com.spark.trouble.HTHistoryFragment
import com.spark.trouble.HTShootRecordActivity
import com.spark.trouble.TroubleDealFragment
import com.spark.trouble.handle.HTinputActivity
import kotlinx.android.synthetic.main.frag_home_admin_manager.*
import me.yokeyword.fragmentation.SupportFragment
import java.util.*

/**
 * 管理员界面
 */
class HomeAdminManagerFragment : BaseFragment() {
    override fun contentResId(): Int {
        return R.layout.frag_home_admin_manager
    }

    private val func1List = ArrayList<String>()
    private val func2List = ArrayList<String>()

    private var onBackClickListener: OnBackClickListener? = null


    companion object {
        fun newInstance(): HomeAdminManagerFragment {
            val args = Bundle()
            val fragment = HomeAdminManagerFragment()
            fragment.arguments = args
            return fragment
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        func1List.add("纠违录入")
        func1List.add("纠违管理")
        func1List.add("纠违历史")

        func2List.add("隐患录入")
        func2List.add("排查记录")
        func2List.add("隐患处理")
        func2List.add("隐患历史")
        func2List.add("督办管理")


        tv_title.text = "早上好 ${Const.getUser()?.user?.name}"


        ll_admin_home.setOnClickListener {
            if (onBackClickListener != null) {
                onBackClickListener!!.onBackClicked()
            }
        }
        // function one
        val funcAdapter1 = FuncAdapter(R.layout.item_admin_func, func1List).apply {
            setOnItemClickListener { adapter, view, position ->
                val str = adapter.data[position]
                when (str) {
                    "纠违录入" -> {
                        activity?.apply { RectInputActivity.actionStart(this) }
                    }
                    "纠违管理" -> {
                        parentFragment?.parentFragment?.let { it as SupportFragment }?.start(RectifyManageFragment.newInstance())
                    }
                    "纠违历史" -> {
                        parentFragment?.parentFragment?.let { it as SupportFragment }?.start(RectHistoryFragment.newInstance())
                    }
                }
            }
        }
        recy_admin_func1.apply {
            adapter = funcAdapter1
            layoutManager = object : GridLayoutManager(context, 3) {
                override fun canScrollVertically(): Boolean {
                    return false
                }

                override fun canScrollHorizontally(): Boolean {
                    return false
                }
            }
        }
        // function two
        val funcAdapter2 = FuncAdapter(R.layout.item_admin_func, func2List).apply {
            setOnItemClickListener { adapter, view, position ->
                val str = adapter.data[position]
                when (str) {
                    "隐患录入" -> {
                        activity?.apply { HTinputActivity.actionStart(this) }
                    }
                    "隐患处理" -> {
                        parentFragment?.parentFragment?.let { it as SupportFragment }?.start(TroubleDealFragment.newInstance())
                    }
                    "隐患历史" -> {
                        parentFragment?.parentFragment?.let { it as SupportFragment }?.start(HTHistoryFragment.newInstance())
                    }
                    "排查记录" -> {
                        activity?.apply { HTShootRecordActivity.actionStart(this) }
                    }
                    // "督办管理"
                    else -> {
                        alert("功能正在开发，敬请期待!")
                    }

                }
            }
        }
        recy_admin_func2.apply {
            adapter = funcAdapter2
            layoutManager = object : GridLayoutManager(context, 3) {
                override fun canScrollVertically(): Boolean {
                    return false
                }

                override fun canScrollHorizontally(): Boolean {
                    return false
                }
            }
        }

    }

    override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation {
        return CubeAnimation.create(CubeAnimation.LEFT, enter, 300)
    }

    /**
     * function adapter
     */
    internal inner class FuncAdapter(layoutResId: Int, data: List<String>?) : BaseQuickAdapter<String, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: String) {
            helper.setText(R.id.tv_func_name, item)
        }
    }

    /**
     * 回退监听
     */
    interface OnBackClickListener {
        fun onBackClicked()
    }

    fun setOnBackClickListener(listener: OnBackClickListener?) {
        if (listener == null) {
            return
        }
        this.onBackClickListener = listener
    }
}
